package com.binguo.thirdPay.diKeYWPay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yuritian
 * @Title: diKeYWPay
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2019-08-1609:17
 */
@Service
public class DiKeYWPay {

    private final String ORGID = "2";

    private final String URL = "http://47.93.19.150:8180/face/business";

    public static void main(String[] args) {
        DiKeYWPay diKeYWPay = new DiKeYWPay();
        if (diKeYWPay.faceConsume("192.168.16.172","3C-97-0E-42-3F-06","2019081501","哈工大演示01","124321143211","1")){
            System.out.println("支付成功");
        }else {
            System.out.println("支付失败");
        }
    }

    public Boolean faceConsume(String ip,
                            String mac,
                            String idserial,
                            String username,
                            String posjourno,
                            String txamt){
        Map<String, String> messageMap = new HashMap<>();
        Date date = new Date();
        SimpleDateFormat txtFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        messageMap.put("orgid", ORGID);
        messageMap.put("ip", ip);
        messageMap.put("mac", mac);
        messageMap.put("txtime", txtFormat.format(date));
        messageMap.put("idserial", idserial);
        messageMap.put("username",username);
        messageMap.put("posjourno",posjourno);
        messageMap.put("txamt",txamt);
        messageMap.put("pospattern","1");
        System.out.println(JSON.toJSONString(messageMap));
        Base64 base64 = new Base64();
        String body = base64.encodeToString(JSON.toJSONString(messageMap).getBytes());
        String method = "faceConsume";
        JSONObject httpJson = new JSONObject();
        httpJson.put("method",method);
        httpJson.put("body",body);

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.setAccept(Lists.newArrayList(MediaType.APPLICATION_JSON));
        String message = JSON.toJSONString(httpJson);
        HttpEntity<String> entity = new HttpEntity<String>(message, headers);
        ResponseEntity<String> resp = restTemplate.postForEntity(URL, entity, String.class);
        String result = resp.getBody();
        System.out.println(result);
        JSONObject httpResult = JSON.parseObject(result);
        Boolean payResult = httpResult.getBoolean("returncode");
        return payResult;
    }
}
